<?php
namespace app\common\model;

use think\Model;
use think\Db;
use think\Exception;

class ShopModel extends Model
{

    protected $resultSetType = 'collection';//返回数组

    public function __construct($data = []) {
        parent::__construct($data);
    }

    /**
     * @description 获取店铺所有数据列表
     * @author cxh
     * @param int $count
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function search_shop($where,$latitude,$longitude){
        $EARTH=6378.137; //地球半径
        $PI=3.1415926535898; //PI值
        $result = $this
            ->table('ds_shop')
            ->where($where)
            ->field("*,(2 * $EARTH* ASIN(SQRT(POW(SIN($PI*(".$latitude."-shop_latitude)/360),2)+COS($PI*".$latitude."/180)* COS(shop_latitude * $PI/180)*POW(SIN($PI*(".$longitude."-shop_longitude)/360),2)))) as juli")
            ->order('juli desc')
            ->toArray();
        return $result;
    }

    public function get_shop_all_list($count = 0) {
        $where = [];
        if ($count > 0) {
            $result['count'] = $this->table('ds_shop')->where($where)->count();
        } else {
            $result = $this
                ->table('ds_shop')
                ->where($where)
                ->order('shop_id desc')
                ->field('*')
                ->select()
                ->toArray();
        }
        return $result;
    }
    public function get_shop_List($latitude,$longitude,$array){
        $EARTH=6378.137; //地球半径
        $PI=3.1415926535898; //PI值
        $result = $this
             ->table('ds_shop')
            ->where($array)
            ->field("*,(2 * $EARTH* ASIN(SQRT(POW(SIN($PI*(".$latitude."-shop_latitude)/360),2)+COS($PI*".$latitude."/180)* COS(shop_latitude * $PI/180)*POW(SIN($PI*(".$longitude."-shop_longitude)/360),2)))) as juli")
            ->order('juli desc')
            ->toArray();
        return $result;
    }
    public function get_shop_list_detail($latitude,$longitude,$array,$shop_class_id){
        //$EARTH=6378.137; //地球半径
        //$PI=3.1415926535898; //PI值
        // ->field("*,(2 * $EARTH* ASIN(SQRT(POW(SIN($PI*(".$latitude."-shop_latitude)/360),2)+COS($PI*".$latitude."/180)* COS(shop_latitude * $PI/180)*POW(SIN($PI*(".$longitude."-shop_longitude)/360),2)))) as juli")
        $sql1 = "(round(6367000 * 2 * asin(sqrt(pow(sin(((shop_latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2) + cos(({$latitude} * pi()) / 180) * cos((shop_latitude * pi()) / 180) * pow(sin(((shop_longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
        $sql = "select a.*,b.*,{$sql1} from 'ds_shop_class' as a left join 'ds_shop' as b on a.shop_id = b.shop_id 
        WHERE `b.shop_latitude` >= {$array['right-bottom']['lat']} 
                AND `b.shop_latitude` <= {$array['left-top']['lat']} 
                AND `b.shop_longitude` >= {$array['left-top']['lng']} 
                AND `b.shop_longitude` <= {$array['right-bottom']['lng']}
                AND `a.shop_class_id` = {$shop_class_id}
                order by distance asc ";
        $data = Db::query($sql);
        $EARTH=6378.137; //地球半径
        $PI=3.1415926535898; //PI值
        $result = $this
            ->table('ds_shop_class')
            ->alias('a')
            ->leftJoin('ds_shop b','a.shop_id = b.shop_id')
            ->where($array)
            ->field("*,(2 * $EARTH* ASIN(SQRT(POW(SIN($PI*(".$latitude."-shop_latitude)/360),2)+COS($PI*".$latitude."/180)* COS(shop_latitude * $PI/180)*POW(SIN($PI*(".$longitude."-shop_longitude)/360),2)))) as juli")
            ->order('juli desc')
            ->toArray();
        return $result;
    }

    /**
     * @description 获取店铺分页列表
     * @author cxh
     * @param int $organization_id
     * @param string $article_name
     * @return array
     * @throws \think\exception\DbException
     */
    public function get_shop_paginate_list($where = [],$page,$size) {
        $result = $this
            ->table('ds_shop')
            ->where($where)
            ->order('shop_id desc')
            ->field('*')
            ->page($page,$size)
            ->toArray();
        return $result;
    }


    /**
     * @description 根据shop_id获取店铺单条数据
     * @author cxh
     * @param int $shop_id
     * @return array|false|\PDOStatement|string|Model
     */
    public function get_shop_data($shop_id) {
        try{
            $result = $this
                ->table('ds_shop')
                ->where('shop_id', $shop_id)
                ->find();
            if (!empty($result)) {
                $result = $result->toArray();
            }
            return $result;
        }catch (Exception $e){
            return [];
        }

    }



    /**
     * @description 添加店铺
     * @author cxh
     * @param $data
     * @return array
     */
    public function add_shop($data) {
        $data['create_at'] = time();
        $result = $this->table('ds_shop')->insert($data);
        if ($result) {
            return ['shop_id' => $this->getLastInsID()];
        } else {
            return [];
        }
    }

    /**
     * @description 更新店铺
     * @author cxh
     * @param $shop_id
     * @param $data
     * @return $this|int|string
     */
    public function update_shop($shop_id, $data) {
        $where = array(
            'shop_id' => $shop_id
        );
        $data['update_at'] = time();
        $result = $this->table('ds_shop')->where($where)->update($data);
        return $result;
    }

}
